import { cacheTools, promptBox, toast } from 'nc-lightapp-front';
export default class headerAction {
	constructor(comp) {
		this.comp = comp;
	}
	backToList = () => {
		const { action, props } = this.comp;
		const { dispatch,button,transDimission } = props;
		dispatch({
			type: 'transDimission/update',
			payload: {
				showMode: 'list-browse'
			}
		});
		if(transDimission.widget){
			button.setButtonVisible({
				search: false,
				approve: false
			});
			action.initAction.getWidgetTableData();
		}else{
			action.initAction.setTableData();
		}
		
	};
	searchChange = (val) => {
		const { props, action } = this.comp;
		const { dispatch,button} = props;
		if(val == 'widget'){
			dispatch({
				type: 'transDimission/update',
				payload: {
					hrorgobj: {},
					widget: true
				}
			});
			setTimeout(() => {
				action.initAction.getWidgetTableData();
			}, 100);
		}else{
			if(val){
				button.setButtonVisible({
					search: true,
				});
			}
			dispatch({
				type: 'transDimission/update',
				payload: {
					hrorgobj: val,
					widget:false
				}
			});
			setTimeout(() => {
				action.initAction.setTableData();
			}, 100);
		}	
	};
	openFilterModal = () => {
		const { props } = this.comp;
		const { search } = props;
		search.openAdvSearch('searchform', true);
	};
	approve = () => {
		const { props } = this.comp;
		const { dispatch, transDimission, cardPagination, form, cardTable } = props;
		let params = {
			type: transDimission.pagiType,
			pk_org: transDimission.hrorgobj.refpk,
			ischeck: transDimission.tabIndex == 1 ? 'N' : 'Y',
			handoverdetailgrid: cardTable.getAllData('handoverdetailmygrid')
		};
		let billId = cardTable.getValByKeyAndIndex('handoverdetailmygrid', 0, 'pk_bill').value;
		dispatch({
			type: 'transDimission/sendApprove',
			payload: {
				postData: params
			}
		}).then((res) => {
			dispatch({
				type: 'transDimission/update',
				payload: {
					showMode: 'card-browse'
				}
			});
			let hasDatas = JSON.stringify(res.data) === '{}' ? false : true;
			cardTable.setStatus('handoverdetailmygrid', 'browse');

			if (hasDatas) {
				form.setAllFormValue({ psninfoheadform: { rows: res.data.head['psninfoheadform'].rows } });
				cardTable.setTableData('handoverdetailmygrid', res.data.bodymycheck['handoverdetailgrid']);
				cardTable.setTableData('handoverdetailgrid', res.data.bodyall['handoverdetailgrid']);
				cardPagination.setCardPaginationId({ id: billId, status: 1 });
			}
		});
	};
	handlePageInfoChange = (prop, flag, status = 1) => {
		const { props, action } = this.comp;
		const { cardPagination, form, editTable } = props;
		if (flag) {
			cardPagination.setCardPaginationId({ id: flag, status: status });
		} else {
			form.EmptyAllFormValue('psninfoheadform');
			editTable.setTableData('handoverdetailgrid', []);
			return;
		}
		if (status === 3) {
			return false;
		}
		var allpks = cacheTools.get('allpks');
		if (!allpks.length) {
			form.EmptyAllFormValue('psninfoheadform');
			editTable.setTableData('handoverdetailgrid', []);
		} else {
			allpks.forEach((item, index) => {
				if (item === flag) {
					setTimeout(() => {
						action.cardAction.setDetailData(item, index);
					}, 100);
				}
			});
		}
	};
}
